package vip.liux.contracts.shared.kits;

import vip.liux.contracts.shared.value.FileInfo;

import java.util.Set;
import java.util.regex.Pattern;

public record FileValidator(long size, Set<Pattern> includeFormatPatterns) {
    public boolean validate(FileInfo fileUpload) {
        return checkSize(fileUpload.getSize()) && checkFormat(fileUpload.getExtensionName());
    }

    private boolean checkSize(long size) {
        return size <= this.size;
    }

    private boolean checkFormat(String extensionName) {
        return includeFormatPatterns.stream()
                .anyMatch(pattern -> pattern.matcher(extensionName).matches());
    }
}
